package com.sgb.jicai.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.FragmentJicaiQiyeZhuanxiangBinding;
import com.sgb.jicai.viewmodel.JiCaiEnterpriseSpecialModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.view.adapter.CentralizedMiningCuttingLineAdapter;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/6/1
 * AUTHOR:F_Q
 * Description://即报即采-汇集采购-企业专项
 */
public class JiCaiEnterpriseSpecialFragment  extends AppLazyFragment {
    private static String TYPE = "type";
    JiCaiEnterpriseSpecialModel mModel;
    FragmentJicaiQiyeZhuanxiangBinding mBind;
    /**
     * 布句列表适配器  CentralizedMining2Adapter
     */
    private CentralizedMiningCuttingLineAdapter centralizedMiningAdapter;
    /**
     * 传递字段信息
     */
    private String type ="";//是否展示集采方式
    private String groupbuyMode = ""; //集采类型：1,需方集采，2企业专项

    private static String GROUPBUY_TYPE = "groupbuy_type";
    private ArrayList<Fragment> mFragments;


    /**
     * 需方发起 和企业专项公用列表
     *
     * @param groupbuyMode 集采类型：1,需方集采，2企业专项
     * @param type 是否展示集采方式 1、展示 2、不展示
     * @return
     */
    public static JiCaiEnterpriseSpecialFragment newInstance(String groupbuyMode,String type) {
        JiCaiEnterpriseSpecialFragment fragment = new JiCaiEnterpriseSpecialFragment();
        Bundle bundle = new Bundle();
        bundle.putString(GROUPBUY_TYPE, groupbuyMode);
        bundle.putString(TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_jicai_qiye_zhuanxiang, container, false);
        mModel = new ViewModelProvider(this).get(JiCaiEnterpriseSpecialModel.class);
        mModel.setActivity(JiCaiEnterpriseSpecialFragment.this);
        groupbuyMode = getArguments().getString(GROUPBUY_TYPE);
        type = getArguments().getString(TYPE);
        return mBind.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        mModel.groupBuyClassList();
    }




    public void showEmptyView() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void updateSecondTabData(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            showEmptyView();
            return;
        }
        //添加  全部
        PickSelectEntity entity = new PickSelectEntity();
        entity.setProjectscale("全部");
        entity.setNo("");
        data.add(0, entity);
        updataTab(data);
    }
    private void updataTab(List<PickSelectEntity> dataTabSecond) {
        if (null == dataTabSecond||dataTabSecond.size()==0) {
            return;
        }
        String[] mTitles = new String[dataTabSecond.size()];
        mFragments = new ArrayList<>();
        for (int i = 0; i < dataTabSecond.size(); i++) {//类型：商品集采、机械集采、设备集采…………
            mTitles[i]=(dataTabSecond.get(i).getProjectscale());
            mFragments.add(JiCaiQiYeFragment.newInstance(dataTabSecond.get(i).getNo().isEmpty()?0:Integer.valueOf(dataTabSecond.get(i).getNo()),groupbuyMode,type,"2"));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitles);
        mBind.viewPager.setOffscreenPageLimit(mTitles.length);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.viewPager.setNoScroll(true);
        mBind.tabLayout.setupWithViewPager(mBind.viewPager);
        mBind.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBind.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabDataList();
                initializePop();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void tabDataList() {
        Fragment fragment = mFragments.get(mBind.viewPager.getCurrentItem());
        if (fragment instanceof JiCaiXuFangFaQiFragment) {
            ((JiCaiXuFangFaQiFragment) fragment).getData();
        }
    }

    private void initializePop() {
        Fragment fragment = mFragments.get(mBind.viewPager.getCurrentItem());
        if (fragment instanceof JiCaiXuFangFaQiFragment) {
            ((JiCaiXuFangFaQiFragment) fragment).initializePop();
        }
    }
    public void getData() {
        mBind.viewPager.setCurrentItem(0);
        if (mBind.tabLayout.getTabAt(0)!=null){
            mBind.tabLayout.getTabAt(0).select();
            tabDataList();
            initializePop();
        }

    }
}
